'use strict';

angular.module('snPaas_om').service('mysqlMonitorStore',['SpHttp','$q','baseUrl',function($http,$q,baseUrl){
	
	var clusterMonit = function(id){
		var deferred = $q.defer();
		$http.get('/rest/mysqlCluster/component',{params:{mysqlClusterId:id}}).success(function(data,status){
			deferred.resolve(data.data);
		});
		return deferred.promise;
	};
	var componentMonit = function(id,comp){
		var deferred = $q.defer();
		$http.get('/rest/mysqlCluster/monitor',{params:{mysqlClusterId:id,component:comp}}).success(function(data,status){
			deferred.resolve(data.data);
		});
		return deferred.promise;
	};




	var _optFn = function(params,deferred){
		$http({
			    method: 'POST',
			    url: baseUrl.ajax+'/rest/component/operateComponent',
			    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
			    data: $.param(params),
			}).success(function (resp) {
				deferred.resolve(resp);
			}).error(function(resp){
				deferred.reject(resp);
			});
	};

	var start = function(index,component,deployId){
		var deferred = $q.defer();
		var params = {
			index:index,
			component:component,
			deployId:deployId,
			operation:'started'
		};
		_optFn(params,deferred);
		
		return deferred.promise;
	};
	var stop = function(index,component,deployId){
		var deferred = $q.defer();
		var params = {
			index:index,
			component:component,
			deployId:deployId,
			operation:'stopped'
		};
		_optFn(params,deferred);
		
		return deferred.promise;
	};
	var restart = function(index,component,deployId){
		var deferred = $q.defer();
		var params = {
			index:index,
			component:component,
			deployId:deployId,
			operation:'restart'
		};
		_optFn(params,deferred);
		
		return deferred.promise;
	};

	return {
		clusterMonit:clusterMonit,
		componentMonit:componentMonit,
		start:start,
		stop:stop,
		restart:restart
	};
}]);